package com.zshan.clinic.web.controller.config;

import com.zshan.clinic.admin.entity.dto.FileUploadDTO;
import com.zshan.clinic.admin.service.config.FileUploadService;
import com.zshan.clinic.common.enums.YesOrNoEnum;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.file.BASE64DecodedMultipartFile;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
public class FileController extends AbstractController {

    @Autowired(required = false)
    private FileUploadService fileUploadService;

    /**
     * 文件BASE64上传
     * @param fileUploadDTO
     * @return
     */
    @RequestMapping(value = "/base64/upload",method = RequestMethod.POST)
    public RestResp<String> base64Upload(@RequestBody FileUploadDTO fileUploadDTO) {
        UserInfo userInfo = getUserInfo();
        fileUploadDTO.setUserId(userInfo.getEmpId());
        fileUploadDTO.setUserName(userInfo.getEmpName());
        String url = fileUploadService.uploadMultipartFile(BASE64DecodedMultipartFile.base64ToMultipart(fileUploadDTO.getBaseStr()),fileUploadDTO.getUserId(),fileUploadDTO.getUserName(),fileUploadDTO.getIsCompress());
        return RestResp.SUCCESS(url);
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public RestResp<String> upload(@RequestParam(value = "file") MultipartFile file) {
        UserInfo userInfo = getUserInfo();
        String url = fileUploadService.uploadMultipartFile(file,userInfo.getEmpId(), userInfo.getEmpName(),YesOrNoEnum.YES.getValue());
        return RestResp.SUCCESS(url);
    }
}

